package com.eduspace.eduplatform.wares.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author tbc on 2016/11/21 09:14:13.
 */
@Configuration
@EnableSwagger2
//@AutoConfigureAfter(WebMvcAutoConfiguration.class)
@PropertySource(value = "classpath:application.properties", encoding = "utf-8")
@Slf4j
public class Swagger2Configuration extends WebMvcConfigurerAdapter {
    @Value("${swagger2.api.title:RESTful APIs}")
    private String title;
    @Value("${swagger2.api.description:更多Spring Boot相关文章请关注：<a>http://blog.didispace.com/</a>}")
    private String description;
    @Value("${swagger2.api.termsOfServiceUrl:'http://www.yijiajiao.com.cn'}")
    private String termsOfServiceUrl;
    @Value("${swagger2.api.contact:程序猿DD}")
    private String contact;
    @Value("${swagger2.api.version:1.0}")
    private String version;

    @Value("${swagger2.api.basePackage:com.eduspace.eduplatform.wares.controller}")
    private String basePackage;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .genericModelSubstitutes(DeferredResult.class)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
//                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();


    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
//                .contact(new Contact(contact, "", ""))
                .contact(contact)
                .version(version)
                .build();
    }

    /**
     * 添加静态资源swagger-ui.html，否则请求不到页面
     * SpringBoot默认已经将classpath:/META-INF/resources/和classpath:/META-INF/resources/webjars/映射
     * 所以该方法不需要重写，如果在SpringMVC中，可能需要重写定义
     * 重写该方法需要 extends WebMvcConfigurerAdapter
     *
     * @param registry .
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/*.html")
                .addResourceLocations("/");
    }
}
